// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司 
// SPDX-License-Identifier: GPL-3.0-only 

import { get_admin_session } from "$lib/api/admin_auth";
import { get_user_session, type UserInfo } from "$lib/api/user";

export class UserStore {
    //for user
    public userSession = $state("");
    private _userInfo = $state(null as UserInfo | null);
    public showLoginModal = $state(false);
    public showLogoutModal = $state(false);

    get userInfo() {
        return this._userInfo;
    }

    set userInfo(val: UserInfo | null) {
        this._userInfo = val;
        sessionStorage.setItem("userInfo", JSON.stringify(val));
    }

    //for admin user
    public adminUserSession = $state("");
    private _adminUserName = $state("");
    public showAdminLoginModal = $state(false);

    get adminUserName() {
        return this._adminUserName;
    }

    set adminUserName(val: string) {
        sessionStorage.setItem("adminUserName", val);
        this._adminUserName = val;
    }

    async init() {
        this.userSession = await get_user_session();
        this._userInfo = JSON.parse(sessionStorage.getItem("userInfo") ?? "null");

        this.adminUserSession = await get_admin_session();
        this._adminUserName = sessionStorage.getItem("adminUserName") ?? "";
    }
}

export const userStore = $state(new UserStore());
